package com.woniuxy.course.service.impl;

import com.woniuxy.commons.dto.CourseDto;
import com.woniuxy.commons.dto.CoursewareDto;
import com.woniuxy.commons.entity.Course;
import com.woniuxy.commons.entity.Courseware;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.course.mapper.CoursewareMapper;
import com.woniuxy.course.service.CoursewareService;
import com.woniuxy.course.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 王光强
 * @date 2020/7/20 0020-19:31
 */
@Service
public class CoursewareServiceImpl implements CoursewareService {

    @Autowired
    private CoursewareMapper coursewareMapper;

    /**
     * @return 增加课件
     * @Author 王光强
     */
    @Override
    public int addCourseware(Courseware courseware) {
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        Date date = new Date();  //获取当前时间
//        courseware.setUpdateTime(df.format(date));
        return coursewareMapper.addCourseware(courseware) > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;
    }

    @Override
    public List<Courseware> findAll() {
        return coursewareMapper.findAll();
    }


    /**
     * @Author 王光强
     * @return
     * 通过cwid查询所有课件
     */
    public Courseware findCoursewareByCwid(int cwid){
        return coursewareMapper.findCoursewareByCwid(cwid);
    };


    /**
     * @return 删除课件
     * @Author 王光强
     */
    @Override
    public int delCoursewareByCwid(int cwid) {
        return coursewareMapper.delCoursewareByCwid(cwid) > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;
    }

    /**
     * @return 批量删除课件
     * @Author 王光强
     */
    @Override
    public int delCoursewareByAnyCwid(int[] cwids) {
        int code = 0;
        for (int i = 0; i < cwids.length; i++) {
            code = coursewareMapper.delCoursewareByCwid(cwids[i]);
            if (code == 0) {
                break;
            }
        }
        return code > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;

    }


    /**
     * @param courseware
     * @return 修改课件
     * @Author 王光强
     */
    @Override
    public int updateCoursewareByCwid(Courseware courseware) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Date date = new Date();  //获取当前时间
        courseware.setUpdateTime(df.format(date));
        return coursewareMapper.updateCoursewareByCwid(courseware) > 0 ? StatusCode.OPERATION_SUCCESS : StatusCode.OPERATION_FAIL;
    }

    /**
     * @param condition
     * @return 多条件分页查询
     * @Author 王光强
     */
    @Override
    public PageBean<CoursewareDto> findCoursewareByCondition(CoursewareDto condition) {
        List<CoursewareDto> results = new ArrayList<>();
        int pageIndex = condition.getIndex();
        int pageSize = condition.getPageSize();
        if (pageIndex < 1) {
            pageIndex = 1;
        }
        condition.setIndex((pageIndex - 1) * pageSize);
        List<CoursewareDto> coursewareByCondition = coursewareMapper.findCoursewareByCondition(condition);
        int totalNums = coursewareByCondition.size();
        int totalPage = totalNums % pageSize == 0 ? totalNums / pageSize : totalNums / pageSize + 1;
        int fromIndex = (pageIndex - 1) * pageSize;
        int toIndex = fromIndex + pageSize;
        if (toIndex >= totalNums) {
            toIndex = totalNums;
        }
        if (pageIndex > totalPage + 1) {
            fromIndex = 0;
            toIndex = 0;
        }
        results = coursewareByCondition.subList(fromIndex, toIndex);
        //将得到的结果放入pageBean中
        PageBean<CoursewareDto> coursePageBean = new PageBean<>();
        coursePageBean.setPage(pageIndex);
        coursePageBean.setPageSize(pageSize);
        coursePageBean.setData(results);
        coursePageBean.setTotalNum(totalNums);
        coursePageBean.setTotalPage(totalPage);
        coursePageBean.setNumsOfCurrPage(results.size());
        return coursePageBean;
    }
}
